package com.eqdd.yiqidian.fragment;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;

import com.eqdd.annotation.api.DealCodeInjector;
import com.eqdd.annotation.datasave.DealCodeClass;
import com.eqdd.yiqidian.FunctionFragmentCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.BGABannerAdapter;
import com.eqdd.yiqidian.adapter.GridSpacingItemDecoration;
import com.eqdd.yiqidian.adapter.recycleadapter.BaseQuickAdapter;
import com.eqdd.yiqidian.adapter.recycleadapter.BaseViewHolder;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.annotation.DealCode;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.BaseFragment;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.User;
import com.eqdd.yiqidian.bean.rebuild.FuncEnterBean;
import com.eqdd.yiqidian.bean.rebuild.FunctionListBean;
import com.eqdd.yiqidian.bean.rebuild.QiyeImgBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpPresneter;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.http.JsonCallBack;
import com.eqdd.yiqidian.listener.OnMyClickListener;
import com.eqdd.yiqidian.rxbus2.RxBus;
import com.eqdd.yiqidian.ui.gongneng.car.CarActivity;
import com.eqdd.yiqidian.ui.gongneng.daka.DakaActivity;
import com.eqdd.yiqidian.ui.gongneng.kehu.KehuActivity;
import com.eqdd.yiqidian.ui.gongneng.renshi.RenshiActivity;
import com.eqdd.yiqidian.ui.gongneng.renshi.RenshiActivity$$DealCodeInjector;
import com.eqdd.yiqidian.utils.GreenDaoUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.web.WebActivity;
import com.jakewharton.rxbinding.view.RxView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by lvzhihao on 17-4-8.
 */

public class FunctionFragment extends BaseFragment {

    private SlimAdapterEx<FunctionListBean> slimAdapterEx;
    private FunctionFragmentCustom dataBinding;

    private int[] resIds = {R.mipmap.banenr1, R.mipmap.banner2,
            R.mipmap.banner3};
    private ArrayList<String> tips;
    private ArrayList<String> imgUrls;
    private Object register;
    private SparseArray<Class> models;

    @DealCodeClass(classes = {RenshiActivity.class})
    public void unpdateRenshi(int num) {
        for (int i = 0; i < slimAdapterEx.getData().size(); i++) {
            if (slimAdapterEx.getDataItem(i).getModule() == 0) {
                slimAdapterEx.getDataItem(i).setNum(num);
                slimAdapterEx.notifyItemChanged(i);
                break;
            }
        }
    }

    @Override
    protected void setView() {
        models = new SparseArray<>();
        models.put(0, RenshiActivity.class);
        models.put(1, KehuActivity.class);
        models.put(2, DakaActivity.class);
        models.put(3, CarActivity.class);
        models.put(4, CarActivity.class);
        OkGo.<HttpResult<List<FunctionListBean>>>post(HttpConfig.BASE_URL + HttpConfig.GET_FUNCTION)
                .params("userGuid", AppConstent.user.getGuid())
                .params("comid", AppConstent.user.getCompanyId())
                .execute(new JsonCallBack<HttpResult<List<FunctionListBean>>>() {
                    @Override
                    public void onSuccess(Response<HttpResult<List<FunctionListBean>>> response) {
                        HttpResult<List<FunctionListBean>> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            slimAdapterEx.updateData(httpResult.getItems());
                            ((FunctionFragment$$DealCodeInjector) register).update1();
                        }
                    }
                });
    }


    @Override
    protected void initData() {
        register = DealCodeInjector.register(this, RxBus.class);
        if (slimAdapterEx == null) {
            dataBinding.recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 5));
            dataBinding.recyclerView.addItemDecoration(
                    new GridSpacingItemDecoration(5, 10, true));
            slimAdapterEx = SlimAdapterEx.create().register(R.layout.grid_item, new SlimInjector<FunctionListBean>() {
                @Override
                public void onInject(FunctionListBean data, IViewInjector injector) {
                    injector.text(R.id.ItemText, data.getText())
                            .image(R.id.ItemImage, data.getIcon())
                            .with(R.id.red_dot, view -> {
                                if (data.getNum() > 0) {
                                    view.setVisibility(View.VISIBLE);
                                } else {
                                    view.setVisibility(View.INVISIBLE);
                                }
                            });
                }
            }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());
            ItemClickSupport.addTo(dataBinding.recyclerView).setOnItemClickListener((rv, p, v) -> {
                startActivity(models.get(slimAdapterEx.getDataItem(p).getModule()));
            });
            dataBinding.bannerViewpager.setAdapter(new BGABannerAdapter(getActivity()));
            dataBinding.bannerViewpager.setData(resIds);
            OkGo.<HttpResult<List<QiyeImgBean>>>post(HttpConfig.BASE_URL + HttpConfig.EQD_IMG_LIST)
                    .execute(new JsonCallBack<HttpResult<List<QiyeImgBean>>>() {
                        @Override
                        public void onSuccess(Response<HttpResult<List<QiyeImgBean>>> response) {
                            HttpResult<List<QiyeImgBean>> httpResult = response.body();
                            ToastUtil.showShort(httpResult.getMsg());
                            if (httpResult.getStatus() == 200) {
                                imgUrls = new ArrayList<>();
                                tips = new ArrayList<>();
                                for (int i = 0; i < httpResult.getItems().size(); i++) {
                                    imgUrls.add(httpResult.getItems().get(i).getImageUrl());
                                    tips.add(httpResult.getItems().get(i).getTitle());
                                }
                                dataBinding.bannerViewpager.setData(imgUrls, tips);
                                dataBinding.bannerViewpager.setDelegate((banner, itemView, model, position) -> {
                                    Bundle bundle = new Bundle();
                                    bundle.putString(Config.TITLE, httpResult.getItems().get(position).getTitle());
                                    bundle.putString(Config.URL, httpResult.getItems().get(position).getUrl());
                                    startActivity(WebActivity.class, bundle);
                                });

                            }
                        }
                    });
        }

    }

    @Override
    public ViewDataBinding initBinding(LayoutInflater inflater) {
        return dataBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_function, null, false);
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onDestroy() {
        DealCodeInjector.unregister(this, RxBus.class, register);
        super.onDestroy();
    }
}